********************************************************************************
** 	TITLE: e25_kites_sentiment_analysis.do

**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XXV: Kites Sentiment Analysis

********************************************************************************
**  PURPOSE: Sentiment analysis for Kites messages 
********************************************************************************
		set sortseed 13

	set scheme burd4

/*********************************************************************************
Panel (a) Positive/Negative/Neutral Sentiment
**********************************************************************************/	
	use "$output_data/kites_sentiment.dta", clear
	
	/**************************************
		B. Bar plot
	**************************************/	
	foreach type in n p nt {
		reg true_`type' post [aw=word_count], `cluster'
		lincom post
		local pval_`type': di %8.3f r(p)
		local pval_`type' = subinstr("`pval_`type''"," ","",.)
		margins, at(post=(0(1)1))
		mat M`type'=r(table)'
	}
	

	
	preserve
	
		svmat Mp 
		svmat Mn
		svmat Mnt

		keep Mp1 Mp5 Mp6 Mn1 Mn5 Mn6 Mnt1 Mnt5 Mnt6
		gen post = _n-1
		gen post2 = post+3
		gen post3 = post2+3
		
		
		keep if !mi(Mp1)
		
		tw (bar Mp1 post if post, fc(green*.7) lc(%0)) (rcap Mp5 Mp6 post if post, lc(black)) ///
			(bar Mp1 post if !post, fc(navy*1.2) lc(%0)) (rcap Mp5 Mp6 post if !post, lc(black)) ///
			(bar Mn1 post2 if post, fc(green*.7) lc(%0)) (rcap Mn5 Mn6 post2 if post, lc(black)) ///
			(bar Mn1 post2 if !post, fc(navy*1.2) lc(%0)) (rcap Mn5 Mn6 post2 if !post, lc(black)) ///
			(bar Mnt1 post3 if post, fc(green*.7) lc(%0)) (rcap Mnt5 Mnt6 post3 if post, lc(black)) ///
			(bar Mnt1 post3 if !post, fc(navy*1.2) lc(%0)) (rcap Mnt5 Mnt6 post3 if !post, lc(black)) ///
			(scatteri .355 -.5 .350 -.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .355 1.5 .350 1.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .355 -.5 .355 1.5 , recast(line) lwidth(medium) lp(-.) lc(black)) ///
			(scatteri .401 2.5 .396 2.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .401 4.5 .396 4.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .401 2.5 .401 4.5 , recast(line) lwidth(medium) lp(-.) lc(black)) ///
			(scatteri .38 5.5 .375 5.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .38 7.5 .375 7.5 , recast(line) lwidth(medium) lp(solid) lc(black)) ///
			(scatteri .38 5.5 .38 7.5 , recast(line) lwidth(medium) lp(-.) lc(black)) ///
			, xlab( 0 "Pre-IGNITE" 1 "Post-IGNITE" 3 "Pre-IGNITE" 4 "Post-IGNITE" 6 "Pre-IGNITE" 7 "Post-IGNITE", labsize(*.65)) ///
			xscale(range(-1 8)) legend(off) ///
			text(.374 .5 "Share positive words", size(*.8)) ///
			text(.364 .5 "{it:p}-value = `pval_p'", size(*.8)) ///
			text(.42 3.5 "Share negative words", size(*.8)) ///
			text(.41 3.5  "{it:p}-value = `pval_n'", size(*.8)) ///
			text(.399 6.5 "Share neutral words", size(*.8)) ///
			text(.389 6.5  "{it:p}-value = `pval_nt'", size(*.8)) ///
			ylab(.23(.05).43) ytitle("Share of words") 
			
	gr export "$figures/kites_pos_neg_neutral.pdf", replace
	
	restore

/*********************************************************************************
Panel (b) Emotion Prevalence, Post- vs. Pre-IGNITE
**********************************************************************************/		
	/**************************************
		C. Coef plot
	**************************************/	
	
	mat R = J(20,4,.)
	local j=1
	foreach v in D_anger D_anticipation D_disgust D_fear D_joy D_sadness D_surprise D_trust{
		
		local sent_`j' = subinstr("`v'","D_","",.)
		
		reg `v' post [aw=word_count], `cluster'
		lincom post
		
		mat R[`j',1] = r(estimate)
		mat R[`j',2] = r(ub)
		mat R[`j',3] = r(lb)
				
		local j=`j'+1
	}
	
	
	preserve
		colorpalette tol BuRd, n(14) nograph
		forval j=1/14{
			local color`j' = r(p`j')
		}
		
		svmat R
		keep R1-R3
		keep if !mi(R1)
		
		gen sent = ""
		forval j=1/8{
			replace sent = proper("`sent_`j''") if _n==`j'
		}
		sort R1 
		gen temp_x = _n
		replace temp_x = 5 if _n==7
		replace temp_x = temp_x+1 if inlist(_n,5,6)
		
		labmask temp_x, values(sent)
		tw (scatter R1 temp_x if temp_x==1, mc("`color3'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==1, lc("`color3'") ) ///
			(scatter R1 temp_x if temp_x==2, mc("`color3'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==2, lc("`color3'") ) ///
			(scatter R1 temp_x if temp_x==3, mc("`color3'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==3, lc("`color3'") ) ///
			(scatter R1 temp_x if temp_x==4, mc("`color3'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==4, lc("`color3'") ) ///
			(scatter R1 temp_x if temp_x==5, mc("`color3'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==5, lc("`color3'") ) ///
			(scatter R1 temp_x if temp_x==6, mc("`color14'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==6, lc("`color14'") ) ///
			(scatter R1 temp_x if temp_x==7, mc("`color14'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==7, lc("`color14'") ) ///
			(scatter R1 temp_x if temp_x==8, mc("`color14'") msymbol(D)) (rspike R2 R3 temp_x if temp_x==8, lc("`color14'") ) ///
			, ///
			yline(0, lc(gs10) lp(solid) lw(*1.5)) xlabel(1/8, valuelabels  labsize(*.8)) ///
			legend(off) ///
			xscale(range(0.25 8.75)) ///
			ylab(-.08(.04).08) yscale(range(-.095 .095)) ///
			xtitle("") ///
			ytitle("Change in word prevalence")  
			
		gr export "$figures/kites_sentiments_diff.pdf", replace
	
	
	restore
	
	